package com.ling.sbbyyds.mapper.news;

import com.ling.sbbyyds.entity.news.Message;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.ArrayList;

@Repository
public interface MessageMapper extends Mapper<Message> {

    /**
     * 获取指定用户的指定好友的未读消息列表
     * @param userId    用户id
     * @param friendId  好友id
     * @return
     */
    @Select("SELECT " +
            "m.send_user_id AS sendUserId,m.send_time AS sendTime,m.message AS message, m.to_user_id as toUserId " +
            "FROM " +
            "sbb_news_message m " +
            "LEFT JOIN sbb_news_message_read r ON r.to_user_id = m.to_user_id " +
            "WHERE " +
            "m.to_user_id = #{userId} " +
            "and send_user_id = #{friendId} " +
            "AND ( " +
            "r.last_read_time IS NULL " +
            "OR m.send_time > r.last_read_time " +
            ") " +
            "order by m.send_time ")
    ArrayList<Message> getUserUnreadNews(@Param("userId") Long userId, @Param("friendId")Long friendId);


    /**
     * 获取指定用户 有未读消息的好友列表
     * @param userId  用户id
     * @return
     */
    @Select("SELECT " +
            "m.send_user_id " +
            "FROM " +
            "sbb_news_message m " +
            "LEFT JOIN sbb_news_message_read r ON r.to_user_id = m.to_user_id " +
            "WHERE " +
            "m.to_user_id = #{userId} " +
            "AND ( " +
            "r.last_read_time IS NULL " +
            "OR m.send_time > r.last_read_time " +
            ") " +
            "group by m.send_user_id")
    ArrayList<Long> getUserUnreadUserList(@Param("userId") Long userId);

}
